package com.jmp.security.grant.crmPc;

import com.jmp.base.dto.SmsCaptchaCheckRequest;
import com.jmp.base.dto.SmsCaptchaCheckResponse;
import com.jmp.base.dto.SmsCodeEnums;
import com.jmp.feign.SmsFeign;
import com.jmp.security.base.ClientDetail;
import com.jmp.security.base.UserDetail;
import com.jmp.security.grant.CardUserDetailValidServiceImpl;
import one.stand.advice.ResultEnum;
import one.stand.config.SecurityProperties;
import one.stand.mapper.CompanyMapperExt;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.util.AssertUtil;

public class CrmPcSmsUserDetailValidServiceImpl extends CardUserDetailValidServiceImpl<CrmPcSmsRequestDetail> {
    private SecurityProperties securityProperties;
    private SmsFeign smsFeign;

    public CrmPcSmsUserDetailValidServiceImpl(BusinessCardBaseService businessCardBaseService, CompanyMapperExt companyMapperExt, SecurityProperties securityProperties, SmsFeign smsFeign) {
        super(businessCardBaseService, companyMapperExt);
        this.securityProperties = securityProperties;
        this.smsFeign = smsFeign;
    }

    @Override
    public void check(CrmPcSmsRequestDetail requestDetail, ClientDetail clientDetail, UserDetail userDetail) {
        if (securityProperties.isSms()) {
            ResultModel<SmsCaptchaCheckResponse> checkResult = smsFeign.captchaCheck(SmsCaptchaCheckRequest.builder()
                    .phone(requestDetail.getPhone())
                    .type(SmsCodeEnums.login_sms.getCode())
                    .captcha(requestDetail.getSmsCode())
                    .build());
            AssertUtil.isTrue(checkResult);
            AssertUtil.isTrue(checkResult.getData().isSuccess(), ResultEnum.FAIL, "验证码错误");
        }

        super.check(requestDetail, clientDetail, userDetail);

        clientDetail.setCompanyKey(userDetail.getCompany().getCompanyKey());
    }
}
